/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import who.tmap.bo.AttrDef;
import who.tmap.bo.Attrs;
import who.tmap.bo.impl.BizObjectFactory;
import who.tmap.common.TMapException;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.PolygonShape;

public class PolygonNamesToAttrsHelper {

	@SuppressWarnings("unchecked")
	public Attrs buildAttrs( List<MapShape> shapes, String polygonColumnName, String dataColumnName, String dataColumnType ) throws TMapException {

		AttrHelper attrHelper =new AttrHelper();
		
        Attrs attrs = (Attrs)BizObjectFactory.getInstance().get( BizObjectFactory.ATTRS );

        List<AttrDef> header =new ArrayList<AttrDef>();
        attrs.setHeader( header );
        
        AttrDef attrDef =(AttrDef)BizObjectFactory.getInstance().get( BizObjectFactory.ATTR_DEF );
        attrDef.setName( polygonColumnName );
        attrDef.setType( String.class.getName() );
        
        header.add( attrDef );
        
        attrDef =(AttrDef)BizObjectFactory.getInstance().get( BizObjectFactory.ATTR_DEF );
        attrDef.setName( dataColumnName );
        attrDef.setType( dataColumnType );
        
        header.add( attrDef );  
        
        List<List> rows =new ArrayList<List>();
        attrs.setRows( rows );
        
        List<String> names =new ArrayList<String>();
        for( MapShape mapShape : shapes ) {
        	if ( mapShape instanceof PolygonShape ) {
        		String name =((PolygonShape)mapShape).getName();
        	
        		if ( !names.contains( name.toUpperCase() )) {
        			names.add( name.toUpperCase() );
        		}
        	}
        }
        
        Collections.sort( names );
        
        for( String name : names ) {
        			
			List<Object> row =new ArrayList<Object>();
			rows.add( row );
			
			row.add( name );
			try {
				Object val =attrHelper.getNewInstanceFromClassName( dataColumnType );
				val =null;
				row.add( val );
				//row.add( attrHelper.getNewInstanceFromClassName( dataColumnType ) );
				//row.add( null );
			} catch (Exception e) {
				e.printStackTrace();
				throw new TMapException( e );
			}
        }
        
        attrs.setDirty( true );
        
		return attrs;
	}
}
